package net.xxcxcxcx.xc_enhancedmob.render.boss_bar.column.client;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.Text;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.column.BaseCustomBossBarColumn;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.CustomBossBar;
import net.xxcxcxcx.xc_enhancedmob.render.boss_bar.unit.client.ClientCustomBossBar;

@Environment(value = EnvType.CLIENT)
public class ClientCustomBossBarColumn extends BaseCustomBossBarColumn implements ClientBaseCustomBossBarColumn {
    public ClientCustomBossBarColumn(NbtCompound nbtCompound) {
        readCustomDataFromNbt(nbtCompound);
    }

    @Override
    public void readCustomDataFromNbt(NbtCompound nbt) {
        for (String s : nbt.getKeys()) {
            if (s.equals("name")) {
                setName(Text.Serializer.fromJson(nbt.getString("name")));
                continue;
            }
            if (s.equals("showname")) {
                setShowname(nbt.getBoolean("showname"));
                continue;
            }
            if (s.equals("uuid")) {
                setUuid(nbt.getUuid("uuid"));
                continue;
            }
            if (s.equals("SortCount")) {
                setSortCount(nbt.getInt("SortCount"));
                continue;
            }
            getBossBars().add(new ClientCustomBossBar(nbt.getCompound(s)));
        }
    }

    //获取当前血条组的像素宽度
    @Override
    public int getWidth() {
        int maxcount = -1;
        for (CustomBossBar customBossBar : getBossBars()) {
            if (customBossBar instanceof ClientCustomBossBar customBossBar1) {
                maxcount = Math.max(maxcount, customBossBar1.getWidth());
            } else break;
        }
        return maxcount;
    }

    //获取当前血条组的像素高度
    @Override
    public int getHeight() {
        int sumcount = 0;
        for (CustomBossBar customBossBar : getBossBars()) {
            if (customBossBar instanceof ClientCustomBossBar customBossBar1) {
                sumcount += customBossBar1.getHeight();
            } else break;
        }
        return sumcount;
    }
}
